import '02_类的构造函数.dart';

main(List<String> args) {
  // final p1 = Person('lyc');
  // final p2 = Person('lyc');

  // print(identical(p1, p2));

  final p1 = Person.withName('lyc');
  final p2 = Person.withName('lyc');

  print(identical(p1, p2));

}

// class Person {
//   final String name;
//   final String color = 'red';

//   const Person(String this.name);
// }

class Person {
  late String name;
  late String color;

  static final Map<String, dynamic> _nameCache = {};
  static final Map<String, dynamic> _colorCache = {};

  factory Person.withName(String name) {
    if (_nameCache.containsKey(name)) {
      return _nameCache[name];
    } else {
      final p = Person(name, "default");
      _nameCache[name] = p;
      return p;
    }
  }

  factory Person.withColor(String color) {
    if (_colorCache.containsKey(color)) {
      return _colorCache[color];
    } else {
      final p = Person('default', color);
      _colorCache[color] = p;
      return p;
    }
  }

  Person(String this.name, String this.color);
}
